<template>
  <div>
    <el-dialog
      width="600px"
      :title="type == 'add' ? $t('新增') : $t('修改')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form @submit.native.prevent ref="formRef" :model="form" label-width="120px" :rules="rules">
        <el-form-item :label="$t('客户名')" prop="receiveName">
          <el-input v-model="form.receiveName" />
        </el-form-item>
        <el-form-item :label="$t('电话')" prop="receivePhone">
          <el-input v-model="form.receivePhone" />
        </el-form-item>
        <el-form-item :label="$t('邮编')" prop="receivePostcode">
          <el-input v-model="form.receivePostcode" />
        </el-form-item>

        <el-form-item :label="$t('是否黑名单')" prop="black">
          <el-select clearable v-model="form.black" style="width: 100%">
            <el-option :label="$t('是')" :value="1" />
            <el-option :label="$t('否')" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('是否线下客户')" prop="offline">
          <el-select clearable v-model="form.offline" style="width: 100%">
            <el-option :label="$t('是')" :value="1" />
            <el-option :label="$t('否')" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('地址1')" prop="address1">
          <el-select filterable v-model="form.address1" clearable style="width: 100%">
            <el-option v-for="item in address1List" :key="item.id" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('地址2')" prop="address2">
          <el-input v-model="form.address2" />
        </el-form-item>
        <el-form-item :label="$t('地址3')" prop="address3">
          <el-input v-model="form.address3" />
        </el-form-item>
        <el-form-item :label="$t('地址4')" prop="address4">
          <el-input v-model="form.address4" />
        </el-form-item>
        <el-form-item :label="$t('地址5')" prop="address5">
          <el-input v-model="form.address5" />
        </el-form-item>
        <el-form-item :label="$t('地址6')" prop="address6">
          <el-input v-model="form.address6" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleSubmit" type="primary">{{ $t('确认') }}</el-button>
        <el-button @click="cancel">{{ $t('关闭') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addOrderClient, address1List } from '@/api/platform/sellerManage'

export default {
  props: {
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      address1List: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receiveName: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        receivePhone: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        // receivePostcode: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        address1: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        black: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        offline: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
        this.getList()
      }
    },
  },
  methods: {
    getList() {
      address1List().then((res) => {
        this.address1List = res.data
      })
    },

    handleSubmit() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          const data = {
            ...this.form,
          }
          // 新增
          if (this.type == 'add') {
            delete data.id
            addOrderClient(data).then((res) => {
              const { code, data, msg } = res
              if (code === 200) {
                this.$message({ type: 'success', message: msg })
                this.cancel()
                this.$emit('getList')
              }
            })
          }
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
